﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Text;

namespace TushuGuan_GUI
{
    public class Book
    {
        const int banben = 1;
        public string ISBN { get; set; } = "";
        public string StudentID { get; set; } = "";

        public bool CanLend
        {
            get
            {
                if (StudentID == "")
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
        }

        public void Lend(string studentId)
        {
            if (StudentID != "")
            {
                throw new Exception("此书已经被" + StudentID + "借走");
            }
            StudentID = studentId;
        }
        public void Return()
        {
            if(StudentID=="")
            {
                throw new Exception("此书不需要归还");
            }
            StudentID = "";
        }

        public string Save()
        {
            string data = new JObject(
                      new JProperty("banben", banben),
                      new JProperty("ISBN", ISBN),
                      new JProperty("StudentID", StudentID)).ToString();
            return data;
        }
        public void Load(string data)
        {
            var json = (JObject)JsonConvert.DeserializeObject(data);
            switch (int.Parse(json["banben"].ToString()))
            {
                case 1:
                    Load_1(data);
                    break;
                default:
                    return;
            }
        }
        private void Load_1(string data)
        {
            var json = (JObject)JsonConvert.DeserializeObject(data);
            var ISBN = json["ISBN"];
            var StudentID = json["StudentID"];

            //保存结果

            this.ISBN = ISBN.ToString();
            this.StudentID = StudentID.ToString();
        }

    }
}
